package aml.bs.spring;

import lombok.Getter;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @Title: SpringCtxHolder
 * @Author duxuqiang02
 * @Date 2023/9/17 13:16
 * @description: spring重写上下文 用以静态捕获nacos配置
 */
@Component
public class SpringCtxHolder implements ApplicationContextAware {

    @Getter
    private static ApplicationContext applicationContext = null;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (SpringCtxHolder.applicationContext == null) {
            SpringCtxHolder.applicationContext = applicationContext;
        }
    }

    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    public static <T> T getBean( Class<T> clazz,String beanName) {
        return getApplicationContext().getBean(beanName, clazz);
    }
}
